// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/benchmark.h>
#include <melon/init/init.h>

#include <pollux/benchmarks/ExpressionBenchmarkBuilder.h>
#include <pollux/common/base/pollux_exception.h>
#include <pollux/expression/complex_view_types.h>
#include <pollux/functions/registerer.h>
#include <pollux/functions/udf.h>
#include <pollux/functions/lib/benchmarks/FunctionBenchmarkBase.h>
#include <pollux/type/type.h>
#include <pollux/vector/base_vector.h>
#include <pollux/vector/complex_vector.h>
#include <pollux/vector/decoded_vector.h>

using namespace kumo::pollux;

namespace {
// The following two functions is used to measure the cost of the cast of
// generic view.

// A function that takes array<X> and returns the summation as int64_t. For
// simplicity is here handling double and int64_t.
template <typename T>
struct GenericInputArraySum {
  template <typename TInput>
  void call(int64_t& out, const TInput& arrayOfGeneric) {
    out = 0;
    if (arrayOfGeneric.elementKind() == TypeKind::DOUBLE) {
      for (auto e : arrayOfGeneric) {
        if (e.has_value()) {
          out += e.value().template castTo<double>();
        }
      }
    } else if (arrayOfGeneric.elementKind() == TypeKind::BIGINT) {
      for (auto e : arrayOfGeneric) {
        if (e.has_value()) {
          out += e.value().template castTo<int64_t>();
        }
      }
    } else {
      out = 0;
    }
  }
};

template <typename T>
struct TypedArraySum {
  template <typename TInput>
  void call(int64_t& out, const TInput& typedArray) {
    out = 0;
    for (auto e : typedArray) {
      if (e.has_value()) {
        out += e.value();
      }
    }
  }
};

// The following two functions is used to measure the cost of the cast of
// generic writer when casted to for primitive type.
template <typename T>
struct FullGenericArraySum {
  POLLUX_DEFINE_FUNCTION_TYPES(T);

  void call(
      out_type<Generic<T1>>& outGeneric,
      const arg_type<Array<Generic<T1>>>& arrayOfGeneric) {
    if (arrayOfGeneric.elementKind() == TypeKind::DOUBLE) {
      auto& out = outGeneric.template castTo<double>();
      out = 0;
      for (auto e : arrayOfGeneric) {
        if (e.has_value()) {
          out += e.value().template castTo<double>();
        }
      }
    } else if (arrayOfGeneric.elementKind() == TypeKind::BIGINT) {
      auto& out = outGeneric.template castTo<int64_t>();
      out = 0;
      for (auto e : arrayOfGeneric) {
        if (e.has_value()) {
          out += e.value().template castTo<int64_t>();
        }
      }
    } else {
      POLLUX_UNREACHABLE("not implemented");
    }
  }
};

} // namespace

int main(int argc, char** argv) {
  melon::Init init(&argc, &argv);

  ExpressionBenchmarkBuilder benchmarkBuilder;

  kumo::pollux::
      register_function<FullGenericArraySum, Generic<T1>, Array<Generic<T1>>>(
          {"full_generic_sum"});
  kumo::pollux::register_function<GenericInputArraySum, int64_t, Array<Any>>(
      {"generic_input_sum"});
  kumo::pollux::register_function<TypedArraySum, int64_t, Array<double>>(
      {"typed_sum"});
  kumo::pollux::register_function<TypedArraySum, int64_t, Array<int64_t>>(
      {"typed_sum"});

  benchmarkBuilder
      .addBenchmarkSet(
          fmt::format("array_sum"),
          ROW({"c0", "c1"}, {ARRAY(BIGINT()), ARRAY(DOUBLE())}))
      .addExpression("full_generic_int", "full_generic_sum(c0)")
      .addExpression("generic_input_int", "generic_input_sum(c0)")
      .addExpression("typed_int", "typed_sum(c0)")
      .addExpression("full_generic_double", "full_generic_sum(c1)")
      .addExpression("generic_input_double", "generic_input_sum(c1)")
      .addExpression("typed_double", "typed_sum(c1)");

  benchmarkBuilder.registerBenchmarks();

  melon::runBenchmarks();
  return 0;
}
